import {
  LOGIN_OK,
  LOGION_OUT,
  GET_USER_INFO,
  GET_GOOD_LIST,
  GET_USER_LIST,
  GET_GOOD_DESTAIL,
  GET_GOOD_UPLOAD,
  GOOD_INIT
} from '../actionTypes'

import { 
  fetchLogin,
  fetchgetinfo ,
  fetchgetlist,
  fetchuserlist,
  fetchgoodupoad,
  fetchgooddestail
} from '@/api'



export function login(payload, callback) {
  return dispatch => {
    fetchLogin(payload).then(res=>{
      console.log('token', res)
      localStorage.setItem('token', res.token)
      dispatch({
        type: LOGIN_OK,
        payload: res.token
      })
      callback && callback()    
    })
  }
}
// 退出操作
export function logionout (payload) {
  return {
    type: LOGION_OUT,
    payload
  }
}
//获取用户个人信息
export function getinfo() {
  return dispatch => {
    fetchgetinfo().then(res=>{
      console.log('userinfo', res)
      // 把用户信息放到redux中
      dispatch({
        type: GET_USER_INFO,
        payload: res.userInfo
      })
    })
  }
}

//获取商品列表业务
export function getgoodlist(payload){
  return dispatch =>{
    fetchgetlist(payload).then(res=>{
      console.log(res);
      dispatch({
        type: GET_GOOD_LIST,
        payload: res

      })
      
    })
  }
}


//获取用户列表

export function getuserlist(payload){
  return dispatch =>{
    fetchuserlist(payload).then(res=>{
      console.log(res);
      dispatch({
        type: GET_USER_LIST,
        payload: res.list

      })
      
    })
  }
}


// 文件上传
export function getgoodupload (payload){
  return dispatch =>{
    fetchgoodupoad(payload).then(res=>{
      console.log(res);
      dispatch({
        type:GET_GOOD_UPLOAD,
        payload:''
      })
      
    })
  }
}


// 获取商品详情
export function getgooddestail (payload){
  return dispatch =>{
    fetchgooddestail(payload).then(res=>{
      console.log(res);
      dispatch({
        type:GET_GOOD_DESTAIL,
        payload:res.info
      })
    })
  }
}


// 清除缓存
export function goodinit(paload){
  return {
    type:GOOD_INIT,
    payload:''
  }
}



